﻿

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>商品列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

    <div class="layui-fluid">

        <div class="layui-card">
            <div class="layui-form layui-card-header layuiadmin-card-header-auto">
                <div class="layui-form-item ">
                    <div class="layui-inline layui-col-lg2">
                        <label class="layui-form-label">经销商名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="customer_name" id="customer_name" placeholder="经销商名称" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline layui-col-lg2">
                        <label class="layui-form-label">款号</label>
                        <div class="layui-input-block">
                            <input type="text" name="productvalue" id="productvalue" placeholder="款号" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline layui-col-lg2">
                        <label class="layui-form-label">品名</label>
                        <div class="layui-input-block">
                            <input type="text" name="productname" id="productname" placeholder="品名" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-inline layui-col-lg2">
                        <label class="layui-form-label">发布日期</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="ksrq" placeholder="yyyy-MM-dd" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline layui-col-lg2">
                        <label class="layui-form-label">-</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" id="jsrq" placeholder="yyyy-MM-dd" autocomplete="off">
                        </div>
                    </div>
                    <div class="layui-inline layui-col-lg1">
                        <button class="layui-btn layuiadmin-btn-useradmin" lay-submit lay-filter="LAY-user-front-search">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </div>

            <div class="layui-card-body">
                <div style="padding-bottom: 10px;">
                    <!--<button class="layui-btn layuiadmin-btn-useradmin" data-type="batchdel">删除</button>-->
                    <!--<button class="layui-btn layuiadmin-btn-useradmin" data-type="add">购物车</button>-->
                    <button class="layui-btn layuiadmin-btn-useradmin" data-type="shopcart">购物车<span id="cartsl" class="layui-badge layui-bg-gray" style="color:red"></span></button>
                    <!--<button class="layui-btn" data-type="shopcart">购物车<span id="cartsl" class="layui-badge layui-bg-gray" style="color:red" ></span></button>-->
                </div>

                <table id="LAY-user-manage" lay-filter="LAY-user-manage"></table>
                <script type="text/html" id="buttonTpl">
                    {{#  if(d.status == 99){ }}
                    <button class="layui-btn layui-btn-xs">已审核</button>
                    {{#  } else if(d.status == 1){ }}
                    <button class="layui-btn layui-btn-primary layui-btn-xs">已发布</button>
                    {{#  } else if(d.status == 0){ }}
                    <button class="layui-btn layui-btn-primary layui-btn-xs">未发布</button>
                    {{#  } else if(d.status == -1){ }}
                    <button class="layui-btn layui-btn-danger layui-btn-xs">驳回</button>
                    {{#  } else { }}
                    <button class="layui-btn layui-btn-primary layui-btn-xs">未审核</button>
                    {{#  } }}
                </script>

                <script type="text/html" id="test-table-operate-td">
                    <a class="layui-btn layui-btn-xs" lay-event="addcart">加入购物车</a>
                    <a class="layui-btn layui-btn-xs" lay-event="nowbuy">直接下单</a>
                </script>

                <script type="text/html" id="imgTpl">
                    <img style="width: 100px; height: 100px;" src={{ d.imageurl }}>
                </script>
            </div>
        </div>
    </div>
    <script src="../../layuiadmin/js/jquery.min2.1.4.js"></script>
    <script src="../../layuiadmin/layui/layui.js"></script>
    <script src="../../layuiadmin/js/cookie.js"></script>

    <script>

        var clickCount = 0;
        layui.config({
            base: '../../layuiadmin/' //静态资源所在路径
        }).extend({
            index: 'lib/index' //主入口模块
        }).use(['index', 'useradmin', 'util', 'table', 'laypage', 'dropdown', 'laydate', 'layer'], function () {
            var $ = layui.$
                , form = layui.form
                , table = layui.table
                , layer = layui.layer
                , laypage = layui.laypage
                , laydate = layui.laydate
                , util = layui.util
                , dropdown = layui.dropdown;

            //加载日期控件
            laydate.render({
                elem: '#ksrq'
            });
            laydate.render({
                elem: '#jsrq'
            });
            //验证用户是否登录
            if ($.cookie("loginid") == "" || $.cookie("loginid") == null || $.cookie("loginid") == "null" || $.cookie("loginid") == undefined || $.cookie("loginid") == "undefined") {
                $.removeCookie("id", { path: "/", sucue: true });//用户id
                $.removeCookie("loginid", { path: "/", sucue: true });//登录账号
                $.removeCookie("username", { path: "/", sucue: true });//用户名
                $.removeCookie("role", { path: "/", sucue: true });//权限
                $.removeCookie("islookme", { path: "/", sucue: true });//权限
                window.location.href = "../login/login.html";
            }

            //数据列表加载
            table.render({
                elem: '#LAY-user-manage'
                , id: 'testReload'//容器的ID
                , url: 'ashx/list.ashx?type=getproductlist&userid=' + $.cookie("id")
                , toolbar: '#test-table-radio-toolbarDemo'
                , lineStyle: 'height: 100px'
                , method: 'POST'
                , page: true
                , limits: [5, 10, 20, 30]
                , limit: 5
                , cols: [[
                    { field: 'tmid', hide: true }
                    //, { field: 'imageurl', width: 120, title: '图片', align: 'center', templet: '#imgTpl' }
                    , {
                        title: '图片'
                        , align: "center"
                        , width: 120
                        , templet: function (d) {
                            return '<div ><img src="' + d.imageurl + '" alt="" width="100px" height="100px"  onclick="showBigImage(this)"></a></div>';
                        }
                    }
             
                    , { field: 'sale_code', hide: true }
                    , { field: 'username', width: 130, title: '出售方', align: 'center' }
                    , { field: 'productvalue', width: 150, title: '款号', align: 'center' }
                    , { field: 'color', width: 100, title: '颜色', align: 'center' }
                    , { field: 'cmwm', width: 100, title: '尺码', align: 'center' }
                    , { field: 'nf', width: 100, title: '年份', align: 'center' }
                    , { field: 'jj', width: 100, title: '季节', align: 'center' }
                    , { field: 'sl', width: 100, title: '数量', align: 'center' }
                    , { field: 'createtime', width: 160, title: '发布时间', align: 'center', sort: true }
                    , { width: 200, align: 'center', fixed: 'right', toolbar: '#test-table-operate-td' }
                ]], parseData: function (res) { //将原始数据解析成 table 组件所规定的数据.
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data //解析数据列表
                    };
                }
                , where: {
                    customer_name: $('#customer_name').val()
                    , productvalue: $('#productvalue').val()
                    , productname: $('#productname').val()
                    , ksrq: $('#ksrq').val()
                    , jsrq: $('#jsrq').val()
                }
            });
            function showBigImage(e) {
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 0,
                    shadeClose: true, //点击阴影关闭
                    area: [$(e).width + 'px', $(e).height + 'px'], //宽高
                    content: "<img src=" + $(e).attr('src') + " />"
                });
            }
            getcartsl();
            function getcartsl() {
                $.ajax({
                    url: 'ashx/list.ashx?type=getcartsl&loginids=' + $.cookie("loginid"),
                    type: "post",
                    contentType: "application/json",
                    dataType: "json",
                    data: {},
                    success: function (res) {
                        var index = parent.layer.getFrameIndex(window.name);
                        if (res.flag == "True") {
                            if (res.remsg == "") {
                                $('#cartsl').text(0);
                            } else {
                                $('#cartsl').text(res.remsg);
                            }

                        }
                        else {

                        }
                    }
                });
            }

            //监听排序
            table.on('sort(LAY-user-manage)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
                table.reload('testReload', {//刷新列表
                    initSort: obj //记录初始排序，如果不设的话，将无法标记表头的排序状态。 layui 2.1.1 新增参数
                    , where: { //请求参数
                        field: obj.field //排序字段
                        , order: obj.type //排序方式
                        , docno: $('#docno').val()
                        , customer_name: $('#customer_name').val()
                        , productvalue: $('#productvalue').val()
                        , status: $("select[name='status']").val()
                    }
                });
            });

            //双击行事件
            table.on('rowDouble(LAY-user-manage)', function (obj) {
                //console.log(obj.tr) //得到当前行元素对象
                //console.log(obj.data) //得到当前行数据
                layer.open({
                    type: 2
                    , title: '查看'
                    , content: 'select_view.html?&dj_docno=' + obj.data.docno
                    , maxmin: true
                    , area: [($(window).width() * 0.7) + 'px', ($(window).height() * 0.7) + 'px']
                    , cancel: function (index, layero) {

                    },
                    end: function () {

                    }
                });
                //obj.del(); //删除当前行
                //obj.update(fields) //修改当前行数据
            });
            //监听工具条
            table.on('tool(LAY-user-manage)', function (obj) {
                var data = obj.data;
                //查看明细
                if (obj.event === 'detail') {
                    var index = layer.open({
                        type: 2
                        , title: '查看'
                        , content: 'edit.html?type=view&editid=' + data.id + '&djbhs=' + data.docno
                        , maxmin: true
                        , area: [($(window).width() * 1) + 'px', ($(window).height() * 1) + 'px']
                        , cancel: function (index, layero) {

                        },
                        end: function () {

                        }
                    });
                    layer.full(index);
                }
                //加入购物车
                else if (obj.event === 'addcart') {
                    if ($.cookie("loginid") == data.sale_code) {
                        layer.msg('卖方与买方不能一样！', { icon: 5 });
                        return false;
                    }
                    var yz_zzs = /^[1-9]*[1-9][0-9]*$/;
                    layer.prompt({
                        formType: 0,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                        title: '填写数量',
                        area: ['50px', '100px'],
                        btnAlign: 'c',
                        content: '<div><input name="txt_sl" id="sl" class="layui-input"></div>',
                        yes: function (index, layero) {
                            var value1 = $('#sl').val();//获取数量
                            var kcsl = data.sl;
                            if (!yz_zzs.test(parseInt(value1))) {
                                layer.msg('必须是大于0的正整数！', { icon: 5 });
                                return false;
                            }
                            else if (parseInt(value1) > parseInt(kcsl)) {
                                layer.msg('库存不足！', { icon: 5 });
                                return false;
                            }
                            else {
                                $.ajax({
                                    url: 'ashx/list.ashx?type=addcart&tmids=' + data.tmid + '&sale_code=' + data.sale_code + '&sl=' + value1 + '&loginids=' + $.cookie("loginid"),
                                    type: "post",
                                    contentType: "application/json",
                                    dataType: "json",
                                    data: {},
                                    success: function (res) {
                                        //var index = parent.layer.getFrameIndex(window.name);
                                        if (res.flag == "True") {
                                            layer.msg(res.remsg, {
                                                time: 2000,
                                                end: function () {
                                                    getcartsl();
                                                    layer.close(index);
                                                    //$('#cartsl').text(value1);
                                                    //$("#cartsl").load(value1);
                                                }
                                            })
                                        }
                                        else {
                                            layer.alert(res.remsg);
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
                //直接购买
                else if (obj.event === 'nowbuy') {
                    if ($.cookie("loginid") == data.sale_code) {
                        layer.msg('卖方与买方不能一样！', { icon: 5 });
                        return false;
                    }
                    var htmls = '';
                    htmls += '<div><input name="txt_sl" id="sl" placeholder="购买数量" class="layui-input"></div>';
                    htmls += '<div><input name="txt_name" id="txt_name" placeholder="收货人姓名" class="layui-input"></div>';
                    htmls += '<div><input name="txt_tel" id="txt_tel" placeholder="收货人联系方式" class="layui-input"></div>';
                    htmls += '<div><input name="txt_address" id="txt_address" placeholder="收货人地址" class="layui-input"></div>';
                    var yz_zzs = /^[1-9]*[1-9][0-9]*$/;
                    layer.prompt({
                        formType: 0,//这里依然指定类型是多行文本框，但是在下面content中也可绑定多行文本框
                        title: '填写收货信息',
                        area: ['200px', '300px'],
                        btnAlign: 'c',
                        content: htmls,
                        yes: function (index, layero) {
                            var txt_name = $('#txt_name').val();//姓名
                            var txt_tel = $('#txt_tel').val();//联系方式
                            var txt_address = $('#txt_address').val();//地址
                            var value1 = $('#sl').val();//获取数量
                            var kcsl = data.sl;
                            if (!yz_zzs.test(parseInt(value1))) {
                                layer.msg('必须是大于0的正整数！', { icon: 5 });
                                return false;
                            }
                            else if (parseInt(value1) > parseInt(kcsl)) {
                                layer.msg('库存不足！', { icon: 5 });
                                return false;
                            }
                            if (txt_name === "" || txt_name === null || txt_name === undefined) {
                                layer.msg('收货人姓名不能为空', { icon: 5 });
                                return false;
                            }
                            if (txt_tel === "" || txt_tel === null || txt_tel === undefined) {
                                layer.msg('收货人联系方式不能为空', { icon: 5 });
                                return false;
                            }
                            if (txt_address === "" || txt_address === null || txt_address === undefined) {
                                layer.msg('收货地址不能为空', { icon: 5 });
                                return false;
                            }
                            //主体信息
                            var arrayObj_main = new Array();
                            var main = {
                                "id": $.cookie("id"),
                                "loginid": $.cookie("loginid"),
                                "username": $.cookie("username"),
                                "txt_name": txt_name,
                                "txt_tel": txt_tel,
                                "txt_address": txt_address
                            };
                            arrayObj_main.push(main);
                            //明细
                            var ArrayObj_mx = new Array();

                            var mx = {
                                "tmid": data.tmid,
                                "sale_code": data.sale_code,
                                "sl": value1
                            };
                            ArrayObj_mx.push(mx);

                            if (arrayObj_main.length > 0 && ArrayObj_mx.length > 0) {
                                $.ajax({
                                    type: "post",
                                    async: true,
                                    url: 'ashx/list.ashx?type=buy_save',
                                    data: JSON.stringify({ 'ArrayObj_main': arrayObj_main, 'ArrayObj_mx': ArrayObj_mx }),
                                    dataType: "json",
                                    success: function (res) {
                                        debugger
                                        var index = parent.layer.getFrameIndex(window.name);
                                        if (res.flag == "True") {
                                            clickCount++;
                                            layer.msg(res.remsg, {
                                                time: 2000,
                                                end: function () {
                                                    parent.layer.close(index);
                                                    //window.parent.location.reload();
                                                    window.location.href = "buylist.html";
                                                    //<a href="buylist.html">buylist.html</a>
                                                }
                                            })

                                        }
                                        else {
                                            layer.alert(res.remsg);
                                        }
                                    }
                                })
                            }

                        }
                    });
                }
                //修改
                else if (obj.event === 'edit') {
                    var index = layer.open({
                        type: 2
                        , title: '修改'
                        , content: 'edit.html?type=edit&editid=' + data.id + '&djbhs=' + data.docno
                        , maxmin: true
                        , area: [($(window).width() * 1) + 'px', ($(window).height() * 1) + 'px']
                        , cancel: function (index, layero) {
                            //取消的操作
                            var body = layer.getChildFrame('body', index);
                            var iframeWin = window[layero.find('iframe')[0]['name']];//得到iframe页的窗口对象，执行iframe页的方法：
                            var content = layero.find("iframe")[0].contentWindow.$("#lbl_ysdh").text();
                            if (content == "" || content == null || content == undefined) {

                            }
                            else {
                                //iframeWin.submit('save');//调用子页面的方法，得到子页面返回的ids
                                layer.close(index);//需要手动关闭窗口
                            }
                        },
                        end: function () {
                            table.reload('testReload', {//刷新列表
                                where: { //请求参数
                                    docno: $('#docno').val()
                                    , customer_name: $('#customer_name').val()
                                    , productvalue: $('#productvalue').val()
                                    , status: $("select[name='status']").val()
                                }
                            });
                        }
                    });
                    layer.full(index);
                    //layer.alert('编辑行：<br>' + JSON.stringify(data))
                }
                //复制订单
                else if (obj.event === 'copy') {
                    layer.confirm('您确定要复制吗', function (index) {
                        $.ajax({
                            url: 'ashx/list.ashx?type=copy&getids=' + data.id,
                            type: "post",
                            contentType: "application/json",
                            dataType: "json",
                            data: {},
                            success: function (res) {
                                var index = parent.layer.getFrameIndex(window.name);
                                if (res.flag == "True") {
                                    layer.msg(res.remsg, {
                                        time: 2000,
                                        end: function () {
                                            table.reload('testReload', {//刷新列表
                                                where: { //请求参数
                                                    docno: $('#docno').val()
                                                    , customer_name: $('#customer_name').val()
                                                    , productvalue: $('#productvalue').val()
                                                    , status: $("select[name='status']").val()
                                                }
                                            });
                                        }
                                    })
                                }
                                else {
                                    layer.alert(res.remsg);
                                }
                            }
                        });
                    });
                }
            });
            //监听搜索
            form.on('submit(LAY-user-front-search)', function (data) {
                var field = data.field;
                //执行重载
                table.reload('testReload', {
                    where: {
                        docno: $('#docno').val()
                        , customer_name: $('#customer_name').val()
                        , productvalue: $('#productvalue').val()
                        , status: $("select[name='status']").val()

                    }, page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
            });
            function getRowFexId(value) {
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data, arr = new Array();;
                for (var i = 0; i < data.length; i++) {
                    arr.push(data[i][value]);
                }
                return arr.join(",");
            }
            //事件
            var active = {
                shopcart: function () {
                    var index = layer.open({
                        type: 2,
                        area: ['90%', '95%'],
                        shade: false,
                        maxmin: true,
                        title: '购物车',
                        btn: ["结算", '关闭'],
                        content: 'cartlist.html',
                        yes: function (index, layero) {
                            var objdata = $(layero).find("iframe")[0].contentWindow;
                            $(layero).find("iframe")[0].contentWindow.$('#saveBtn').click();//执行子页面的按钮点击事件
                            var mJson = objdata.$('#uidsub').val();//获取子页面绑定的值
                            if (mJson != "") {
                                var _mJson = $.parseJSON(mJson);//转成Json对象
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='product_value']").text(_mJson[0].product_value);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='id']").text(_mJson[0].id);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='color']").text(_mJson[0].yscode);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='colorname']").text(_mJson[0].ysname);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='size_wm']").text(_mJson[0].size_wm);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='size_nm']").text(_mJson[0].size_nm);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='product_name']").text(_mJson[0].product_name);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='jg']").text(_mJson[0].price);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='nf']").text(_mJson[0].nf);
                                $(".layui-table-box tbody tr[data-index='" + rowIndex + "'] td[data-field='jj']").text(_mJson[0].jj);
                                //同步更新缓存对应的值（真正的更新）
                                obj.update({
                                    product_value: _mJson[0].product_value
                                    , id: _mJson[0].id
                                    , color: _mJson[0].yscode
                                    , colorname: _mJson[0].ysname
                                    , size_wm: _mJson[0].size_wm
                                    , size_nm: _mJson[0].size_nm
                                    , product_name: _mJson[0].product_name
                                    , jg: _mJson[0].price
                                    , nf: _mJson[0].nf
                                    , jj: _mJson[0].jj
                                });
                                table.reload('product_table', {
                                    data: tabledata
                                });
                            }
                        },
                        cancel: function (index, layero) {
                            // 取消的操作
                        },
                        end: function () { }
                    });
                    layer.full(index);
                }
            };
            $('.layui-btn.layuiadmin-btn-useradmin').on('click', function () {
                var type = $(this).data('type');
                active[type] ? active[type].call(this) : '';
            });
        });
    </script>
</body>
</html>
